package edu.neusoft.yuejian_09.activity;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.List;

import edu.neusoft.yuejian_09.R;
import edu.neusoft.yuejian_09.adapter.MasterListAdapter;
import edu.neusoft.yuejian_09.base.RetrofitListener;
import edu.neusoft.yuejian_09.bean.Master;
import edu.neusoft.yuejian_09.model.MasterModel;

public class MasSearchActivity extends Activity implements RetrofitListener<List<Master>> {
    private RecyclerView recyclerView;
    private EditText et_search;
    private Button btn_search;
    private String search_key;
    ImageView img_return;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_mas_search);
        initViews();
        initEvents();
    }

    private  void initViews(){
        et_search = (EditText)findViewById(R.id.search_input);
        btn_search = (Button)findViewById(R.id.search_btn);
        img_return= (ImageView) findViewById(R.id.img_return);
        recyclerView = (RecyclerView)findViewById(R.id.rv_massearch);
    }

    private void initEvents(){
        img_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search_key = et_search.getText().toString();
                getData();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    public void getData() {
        MasterModel masterModel= new MasterModel();
        masterModel.getMasBySearch(search_key,this);

    }

    @Override
    public void onSuccess(final List<Master> data ,int flag){

        System.out.printf("data");
        if ((data.size())==0){
            Toast.makeText(MasSearchActivity.this,"没有搜到任何大师！",Toast.LENGTH_SHORT).show();
        }else{
            MasterListAdapter masterListAdapter=new MasterListAdapter(MasSearchActivity.this,data);

            recyclerView.setAdapter(masterListAdapter);

        }

    }

    @Override
    public void onFail(){ Toast.makeText(MasSearchActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}

}
